// ignore_for_file: file_names, must_be_immutable, constant_identifier_names, slash_for_doc_comments, avoid_unnecessary_containers, prefer_const_constructors, unnecessary_cast, sized_box_for_whitespace, prefer_function_declarations_over_variables, overridden_fields, annotate_overrides, prefer_const_literals_to_create_immutables, unnecessary_null_comparison, deprecated_member_use, prefer_final_fields

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mybase/base/abstract/IFFScaffold.dart';
import 'package:mybase/base/config/AppListTheme.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/tool/pprint/FFLog.dart';
import 'package:mybase/base/widget/layout/FFImageButton.dart';
import 'package:mybase/base/widget/page/FFScaffold.dart';

/**ListView继承此类 */
class FFListScaffold extends FFScaffold {
  FFListScaffold({
    Key? key,
  }) : super(key: key);

  @override
  FFListScaffoldState createState() => FFListScaffoldState();
}

class FFListScaffoldState extends FFScaffoldState with MFFScaffold {
  /// 如果该页面需要与父类联动滚动，该值请赋为null
  ScrollController? scrollController = ScrollController();

  // 列表类型
  FFListScaffoldType ffSliverType = FFListScaffoldType.sliverList;

  double ffSpace = 0; // item间隔
  double ffStartSpace = 0; // 开始间隔
  double ffEndSpace = 0; // 结束间隔

  @override
  void ffInitState() {
    super.ffInitState();
    scrollController?.addListener(ffScrollListener);
  }

  @override
  void ffDispose() {
    scrollController?.removeListener(ffScrollListener);
    scrollController?.dispose();
    super.ffDispose();
  }

  ///监听滚动
  void ffScrollListener() {}

  @override
  Widget? ffBody(BuildContext context) {
    Widget? body2 = ffBody2(context);
    if (body2 != null) return body2;

    Widget? head = ffHeadSliver(context);
    Widget? head2 = ffHeadSliver2(context);
    Widget? foot = ffFootSliver(context);
    List<Widget>? footList = ffFootListSliver(context);
    int childCount = ffItemCount();
    return CustomScrollView(
      controller: scrollController,
      physics: physics,
      slivers: [
        if (head != null) head,
        if (head2 != null) head2,
        if (ffSliverType == FFListScaffoldType.sliverList)
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext ctx, int index) {
                bool isStart = index == 0;
                bool isEnd = index == childCount - 1;
                EdgeInsetsGeometry padding = EdgeInsets.zero;
                padding = EdgeInsets.only(
                    top: isStart ? ffStartSpace : ffSpace * 0.5,
                    bottom: isEnd ? ffEndSpace : ffSpace * 0.5);

                return Padding(
                  padding: padding,
                  child: ffItem(context, index),
                );
              },
              childCount: childCount,
            ),
          ),
        if (ffSliverType == FFListScaffoldType.sliverReorderableList)
          SliverReorderableList(
            itemBuilder: (context, index) {
              bool isStart = index == 0;
              bool isEnd = index == childCount - 1;
              EdgeInsetsGeometry padding = EdgeInsets.zero;
              padding = EdgeInsets.only(
                  top: isStart ? ffStartSpace : ffSpace * 0.5,
                  bottom: isEnd ? ffEndSpace : ffSpace * 0.5);

              return ReorderableDelayedDragStartListener(
                key: ValueKey(index.toString()),
                index: index,
                child: Padding(
                  padding: padding,
                  child: ffItem(context, index),
                ),
              );
            },
            itemCount: childCount,
            onReorder: ffOnReorder,
            proxyDecorator: ffProxyDecorator,
          ),
        if (foot != null) foot,
        if (!FFEmpty.listIsEmpty(footList)) ...footList!.map((e) => e).toList(),
      ],
    );
  }

  /// 有时候需要切换同成不是list的,此时需要设置scrollController为空，不然会报错
  Widget? ffBody2(BuildContext context) {
    return null;
  }

  /**子类重写以下的方法 */
  /**item */
  Widget ffItem(BuildContext context, int index) {
    return FFImageButton(
      image: null,
      heigth: 40,
      color: Colors.pink,
      margin: EdgeInsets.only(
        bottom: 1,
      ),
    );
  }

  /**数量 */
  int ffItemCount() {
    return datas.length;
  }

  // 拖拽
  void ffOnReorder(int oldIndex, int newIndex) {
    FFLog.info("子类需要实现这个方法，老的索引${oldIndex},新的索引${newIndex}");
    // setState(() {
    //   if (newIndex > oldIndex) {
    //     newIndex -= 1;
    //   }
    //   final item = datas.removeAt(oldIndex);
    //   datas.insert(newIndex, item);
    // });
  }

  // 拖拽时的样式
  Widget ffProxyDecorator(
    Widget child,
    int index,
    Animation<double> animation,
  ) {
    return child;
  }

  /**头部 */
  Widget? ffHeadSliver(BuildContext context) {}

  /**头部2 */
  Widget? ffHeadSliver2(BuildContext context) {}

  /**尾声部 */
  Widget? ffFootSliver(BuildContext context) {}

  /**尾声部,这个优先 */
  List<Widget>? ffFootListSliver(BuildContext context) {}
}

enum FFListScaffoldType {
  sliverList,
  sliverReorderableList,
}
